<template>
	<div>
		<a-spin :spinning="spinning">
			<modal-dynamic-form
				ref="modalDynamicForm"
				width="65%"
				@beforeClose="beforeClose"
				@submit="submitModalFrom"
				title="设置信息"
			></modal-dynamic-form>
		</a-spin>
	</div>
</template>

<script>
import ModalDynamicForm from '@/components/DynamicForms/ModalDynamicForms.vue'
import { getFormDesign, save, update, getDetail } from '@/api/site/content'
import { selectOthersMsg, insertOrUpdateOthersMsg } from '@/api/site/category'
export default {
	components: {
		ModalDynamicForm,
	},
	data() {
		return {
			labelCol: {
				xs: {
					span: 24,
				},
				sm: {
					span: 7,
				},
			},
			wrapperCol: {
				xs: {
					span: 24,
				},
				sm: {
					span: 13,
				},
			},
			modalTitle: '',
			rowId: null,
			status: '0',
			// categoryId: null,
			categoryCode: null,
			publishDate: null,
			modelId: null,
			modalOpera: 0, // 0：新增 1：编辑
			visible: false,
			spinning: false,
			supportWehcat: false,
			supportTops: false,
			confirmLoading: false,
			form: this.$form.createForm(this),
			categoryId: '',

			appid: '',
			shortLink: '',
			path: '',
			address: '',
			level: '',
			coordinate: '',
			// covers:'',

			models: [
				{
					check: true,
					fieldCode: 'categoryId',
					fieldName: '栏目id',
					fieldAliase: '栏目id',
					inputType: 'text',
					defaultValue: '',
				},
				{
					check: true,
					fieldCode: 'appid',
					fieldName: 'appid',
					fieldAliase: 'appid',
					inputType: 'text',
					defaultValue: '',
				},
				{
					check: true,
					fieldCode: 'shortLink',
					fieldName: 'shortLink',
					fieldAliase: 'shortLink',
					inputType: 'text',
					defaultValue: '',
				},
				{ check: true, fieldCode: 'path', fieldName: 'path', fieldAliase: 'path', inputType: 'text', defaultValue: '' },
				{
					check: true,
					fieldCode: 'address',
					fieldName: '地址',
					fieldAliase: '地址',
					inputType: 'text',
					defaultValue: '',
				},
				{
					check: true,
					fieldCode: 'level',
					fieldName: '级别',
					fieldAliase: '级别',
					inputType: 'text',
					defaultValue: '',
				},
				{
					check: true,
					fieldCode: 'coordinate',
					fieldName: '坐标',
					fieldAliase: '坐标',
					inputType: 'text',
					defaultValue: '',
				},

				{
					check: true,
					fieldCode: 'tagName',
					fieldName: '标签',
					fieldAliase: '标签',
					inputType: 'tags',
					defaultValue: '',
				},
				{
					check: true,
					fieldCode: 'covers',
					fieldName: '封面',
					fieldAliase: '封面',
					inputType: 'picture',
					defaultValue: '',
				},
				{
					check: true,
					fieldCode: 'false',
					fieldName: '是否卖票',
					fieldAliase: '是否卖票',
					inputType: 'switch',
					defaultValue: '',
				},
				{
					check: true,
					fieldCode: 'baseInfoId',
					fieldName: '',
					fieldAliase: '',
					inputType: 'select',
					defaultValue: '',
					optionList: [],
				},
			],
		}
	},
	created() {},
	methods: {
		add(pmodel, categoryId) {
			this.spinning = true
			this.modelId = pmodel.code
			this.categoryId = categoryId
			this.rowId = null
			this.supportWehcat = pmodel.supportWehcat || false
			this.supportTops = pmodel.supportTops || false
			this.getFormDesign(pmodel.code)
		},
		hideLoading() {
			this.spinning = false
		},
		edit(record) {
			this.categoryId = record.categoryId
			this.categoryCode = record.categoryCode
			this.modelId = record.modelId
			this.rowId = record.id
			this.status = record.status
			this.publishDate = record.publishDate
			this.spinning = true
			// getDetail({
			// 	id: record.id,
			// }).then((response) => {
			// 	if (response.code === 0) {
			// 		console.log(response)
			// 		// this.$refs.modalDynamicForm.initForm(response.res.models)
			// 		this.$refs.modalDynamicForm.initForm(this.models)
			// 		this.spinning = false
			// 	}
			// })

			selectOthersMsg({
				// 这里的id你传的不对
				categoryId: record.id,
			}).then((response) => {
				if (response.code === 0) {
					// this.$refs.modalDynamicForm.initForm(response.res.models)
					// console.log(response.res, 'res')
					if (
						response.res.categoryDto &&
						response.res.categoryDto.baseInfoId &&
						response.res.categoryDto.baseInfoId != -1
					) {
						this.models[9].defaultValue = true
						this.models[10].defaultValue = response.res.categoryDto.baseInfoId
					} else {
						this.models[9].defaultValue = false
					}

					this.models[0].defaultValue = response.res.categoryDto.categoryId
					if (response.res.categoryDto.appid && response.res.categoryDto.appid != 'null') {
						this.models[1].defaultValue = response.res.categoryDto.appid
					} else {
						this.models[1].defaultValue = ''	
					}
					if (response.res.categoryDto.shortLink && response.res.categoryDto.shortLink != 'null') {
						this.models[2].defaultValue = response.res.categoryDto.shortLink
					} else {
						this.models[2].defaultValue = ''	
					}
					if (response.res.categoryDto.path && response.res.categoryDto.path != 'null') {
						this.models[3].defaultValue = response.res.categoryDto.path
					} else {
						this.models[3].defaultValue = ''	
					}
					if (response.res.categoryDto.address && response.res.categoryDto.address != 'null') {
						this.models[4].defaultValue = response.res.categoryDto.address
					} else {
						this.models[4].defaultValue = ''	
					}
					if (response.res.categoryDto.level && response.res.categoryDto.level != 'null') {
						this.models[5].defaultValue = response.res.categoryDto.level
					} else {
						this.models[5].defaultValue = ''	
					}
					if (response.res.categoryDto.coordinate && response.res.categoryDto.coordinate != 'null') {
						this.models[6].defaultValue = response.res.categoryDto.coordinate
					} else {
						this.models[6].defaultValue = ''	
					}
	
			
					// this.models[2].defaultValue = response.res.categoryDto.shortLink
					// this.models[3].defaultValue = response.res.categoryDto.path
					// this.models[4].defaultValue = response.res.categoryDto.address
					// this.models[5].defaultValue = response.res.categoryDto.level
					// this.models[6].defaultValue = response.res.categoryDto.coordinate
					this.models[7].defaultValue = response.res.categoryDto.tagName
					this.models[8].defaultValue = response.res.categoryDto.covers
					// this.models[9].defaultValue = false
					this.models[10].optionList = response.res.baseInfoList

					this.$refs.modalDynamicForm.initForm(this.models)
					this.spinning = false
				}
			})

			// selectOthersMsg({ categoryId: record.id }).then((response) => {
			// console.log(response)
			// if (response.code === 0) {
			// 	console.log(response.res)
			// 	this.form = response.res
			// 	this.spinning = false
			// } else {
			// 	this.$message.error(response.msg)
			// }
			// })
		},
		getFormDesign(id) {
			getFormDesign({ modelId: id }).then((response) => {
				if (response.code === 0) {
					this.$refs.modalDynamicForm.initForm(response.res)
					this.$refs.modalDynamicForm.setModelId(id)
					this.$refs.modalDynamicForm.setSupportTops(this.supportTops)
					this.$refs.modalDynamicForm.setSupportWehcat(this.supportWehcat)
				}
				this.spinning = false
			})
		},
		beforeClose() {
			this.$refs.modalDynamicForm.cleanForm()
		},
		submitModalFrom(data) {
			console.log(data, 'data')
			data.params = Object.assign({}, data)
			data.categoryId = this.categoryId
			data.categoryCode = this.categoryCode
			data.modelId = this.modelId
			data.status = this.status
			data.publishDate = this.publishDate

			if (!this.rowId) {
				const json = JSON.stringify(data)
				save(json).then((response) => {
					this.message(response)
				})
			} else {
				data.id = this.rowId
				data.categoryId = this.rowId
				const json = JSON.stringify(data)
				console.log(data)
				insertOrUpdateOthersMsg(json).then((response) => {
					this.message(response)
				})
			}
		},
		message(response) {
			if (response.code === 0) {
				this.$message.info(response.msg)
				this.$emit('ok')
				this.handleCancel()
			} else {
				this.$message.error(response.msg)
			}
		},
		handleCancel() {
			this.$refs.modalDynamicForm.handleCancel()
			this.supportTops = false
			this.supportWehcat = false
		},
	},
}
</script>
<style scoped>
</style>
